package main.java.com.manger.controller;

import com.manger.constant.Constant;
import com.manger.dto.Page;
import com.manger.dto.Pager;
import com.manger.dto.SystemContext;
import com.manger.entity.Student;
import com.manger.service.StudentService;
import com.manger.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by LCJ on 2017/6/14.
 */
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = Constant.STUDENT_COMMON_URL, method = RequestMethod.POST)
    public String addStudentRecord(Student student, Model model) {
        if (studentService.findStudentByNo(student.getNo()) != null) {
            model.addAttribute(Constant.ATTR_MSG, Constant.USER_EXIST);
            return Constant.PAGE_ERROR;
        }
        return operate(studentService.addStudent(student), model);
    }

    @RequestMapping(value = Constant.STUDENT_ADD_URL, method = RequestMethod.GET)
    public String addStudentRecord() {
        return Constant.PAGE_STUDENT_ADD;
    }

    @RequestMapping(value = Constant.STUDENT_DELETE_URL, method = RequestMethod.GET)
    public String deleteStudentRecord(@PathVariable Integer id, Model model) {
        return operate(studentService.deleteStudentById(id), model);
    }

    @RequestMapping(value = Constant.STUDENT_UPDATE_URL, method = RequestMethod.POST)
    public String updateStudentRecord(Student student, Model model) {
        return operate(studentService.updateStudent(student), model);
    }

    @RequestMapping(value = Constant.STUDENT_UPDATE_URL, method = RequestMethod.GET)
    public String updateStudentRecord(HttpServletRequest httpServletRequest, Model model) {
        String sid = httpServletRequest.getParameter("id");
        Integer id = Integer.parseInt(sid);
        model.addAttribute(Constant.ATTR_DETIALS, studentService.findStudent(id));
        return Constant.PAGE_STUDENT_UPDATE;
    }

    @RequestMapping(value = Constant.STUDENT_FIND_URL, method = RequestMethod.GET)
    public String findStudentById(@PathVariable Integer id, Model model) {
        Student student = studentService.findStudent(id);
        if (student == null) {
            model.addAttribute(Constant.ATTR_MSG, Constant.STUDENT_NULL);
            return Constant.PAGE_ERROR;
        }
        model.addAttribute(Constant.ATTR_STUDENT, student);
        return Constant.PAGE_STUDENT_DETILS;
    }

    @RequestMapping(value = Constant.STUDENT_COMMON_URL, method = RequestMethod.GET)
    public String findAllStudent(Model model, HttpServletRequest request) {
        Page page = PageUtil.pager(request);
        Integer total;
        if (studentService.findCount() % page.getSize() == 0) {
            total = studentService.findCount() / page.getSize();
        } else {
            total = studentService.findCount() / page.getSize() + 1;
        }
        List<Student> students = studentService.findAllStudents(page);
        if (students.isEmpty() || students == null) {
            model.addAttribute(Constant.ATTR_MSG, Constant.STUDENT_NULL);
            return Constant.PAGE_FAILURE;
        }
        model.addAttribute(Constant.ATTR_STUDENT, students);
        model.addAttribute("page", page.getPage());
        model.addAttribute("total", total);
        model.addAttribute("count", studentService.findCount());
        return Constant.PAGE_STUDENT_LIST;
    }

    @RequestMapping(value = "/students/search", method = RequestMethod.POST)
    public String search(HttpServletRequest request,Model model){
        String non=request.getParameter("no");
        Long no=Long.getLong(non);
        List<Student> students=studentService.findStudentByKey(no);
        model.addAttribute("students",students);
        return "admin/reseach";
    }

    @RequestMapping(value = "/students/search", method = RequestMethod.GET)
    public String research(){
        return "admin/reseach";
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(){
        return "test";
    }
    @RequestMapping(value = "/tes", method = RequestMethod.GET)
    public String tes(Model model){

        Pager<Student> students=new Pager<Student>();
        Page page=new Page();
        page.setSize(SystemContext.getPagesize());
        page.setPage(SystemContext.getOffset());
        students.setPageoffset(SystemContext.getOffset());
        students.setPagesize(SystemContext.getPagesize());
        students.setTotal(studentService.findCount());
        students.setDatas(studentService.findAllStudents(page));
        model.addAttribute("pm",students);
        return "test";
    }


    private void addSuccessMessageToModel(Model model) {
        model.addAttribute(Constant.ATTR_MSG, Constant.OPERATEION_SUCCESS);
    }

    private void addFailureMessageToModel(Model model) {
        model.addAttribute(Constant.ATTR_FAILURE, Constant.OPERATEION_FAILURE);
    }

    private String operate(String key, Model model) {
        if (key.equals(Constant.OPERATEION_SUCCESS)) {
            addSuccessMessageToModel(model);
            return Constant.PAGE_SUCCESS;
        }
        addFailureMessageToModel(model);
        return Constant.PAGE_FAILURE;
    }
}
